package com.tpod.taxi.utils;


import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.widget.Toast;

public class UIUtils {

	public static Context context;
	private static Geocoder geocoder;
	private static GPSTracker gps;
	private static String add;

	public static void init(Context context) {
		UIUtils.context = context;
	}

	public static void checkInit(){
		if(context==null)
			throw new RuntimeException("Message: UIUtils must be init before use");
	}

	public static Resources getRes() {
		checkInit();
		return context.getResources();
	}
	
	public static DisplayMetrics getDisplayMetrics(){
		return getRes().getDisplayMetrics();
	}

	public static int convertDpiToPx(int dpi) {		
		return (dpi*getDisplayMetrics().densityDpi)/160;
	}
	
	public static int getScreenWidth(){
		return getDisplayMetrics().widthPixels;
	}
	
	public static int getScreenHeight(){
		return getDisplayMetrics().heightPixels;
	}
	
	
	public static void call(Activity currentActitivy,String number){ 
		Intent intent = new Intent("android.intent.action.CALL");		 
        Uri data = Uri.parse("tel:"+ number );
        intent.setData(data);
        currentActitivy.startActivity(intent);
	}
	
	public static String locationCity(Context context)
	{
		geocoder = new Geocoder(context);
		gps = new GPSTracker(context);

			// check if GPS enabled		
	        if(gps.canGetLocation()){
	        	
	        	double latitude = gps.getLatitude();
	        	double longitude = gps.getLongitude();
	        	
	            try {
	            	  List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
	            	 
	            	  if(addresses != null) {
	            	   Address returnedAddress = addresses.get(0);
	            	  // strReturnedAddress = new StringBuilder("Address:\n");
	            	   add = returnedAddress.getAddressLine(2);
	            	   Toast.makeText(context,"add : "+add, Toast.LENGTH_LONG).show();	
	            	   
	            	  }
	            	  else{
	            		  Toast.makeText(context,"No Address returned!", Toast.LENGTH_LONG).show();	
	            	  }
	            	 } catch (IOException e) {
	            	  // TODO Auto-generated catch block
	            	  e.printStackTrace();
	            	  Toast.makeText(context,"Canont get Address!", Toast.LENGTH_LONG).show();
	            	 }

	        	Toast.makeText(context, "Your Location is - \nLat: " + latitude + "\nLong: " + longitude +"\nadd : "+add, Toast.LENGTH_LONG).show();	
	        }else{
	        	// can't get location
	        	// GPS or Network is not enabled
	        	// Ask user to enable GPS/network in settings
	        	gps.showSettingsAlert();
	        }
		return add;
		
	}
	
}
